package controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RespectBinding;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import BookService.Ebook_categoryService;
import BookService.Ebook_entryService;
import pojo.Ebook_entry;
import tool.Constants;
import tool.PageSupport;

@Controller
@RequestMapping("/book")
public class BookController {
	private Logger logger =Logger.getLogger(BookController.class);
	
	@Resource
	private Ebook_entryService e_book;
	
	
	@RequestMapping(value="/getListBooks",method=RequestMethod.POST)
	@ResponseBody   //此注解为该方法返回类型是json
	public PageSupport getListBooks(       //用户选择查询的分类编号  required=false意思为是否必须的参数
										@RequestParam(value="categoryId",required=false) String categoryId,
											//当前页码
										@RequestParam(value="nowPageNo",required=false) String nowPageNo){
					logger.info("进入getListBooks==========");
					List<Ebook_entry> listBooks;
					//显示页数
					int showSize = Constants.pageSize;
					//设置默认当前页码-来自于用户输入
					int currentPageNo = 1;
					//查询分类编号
					int n_categoryId = 0;
					
					//判断用户传过来的页数，不为空是赋值给currentPageNo
					if (nowPageNo != null && nowPageNo !="") {
						currentPageNo = Integer.valueOf(nowPageNo);
					}
					
					//判断是首次登陆还是用户查找			
					if (categoryId.equals("0")){
						n_categoryId = 0;
					}else if(categoryId != null && categoryId !="" && categoryId != "0"){
						n_categoryId = Integer.valueOf(categoryId);
					}
					
					//用户查找 ： 寻找编号 n_categoryId ， 显示条数 showSize ，第几条开始寻找 = (当前页面-1) * 显示条数
					
					//总页数
					int totalCount = e_book.getConut(n_categoryId);
					PageSupport page = new PageSupport();
						//页面容量
						page.setPageSize(showSize);
						//当前页码-来自于用户输入
						page.setCurrentPageNo(currentPageNo);
						//总数量（表）
						page.setTotalCount(totalCount);
						//总页数
						int totalPageCount = page.getTotalPageCount();
						//控制页数
						if(currentPageNo < 1){
							currentPageNo = 1;
						}else if(currentPageNo > totalPageCount){ 
							currentPageNo = totalPageCount;
						}
						//把控制后的页数重新set到papge里面
						page.setCurrentPageNo(currentPageNo);
						//用户查找 ： 寻找编号 n_categoryId ， 显示条数 showSize ，statr第几条开始寻找 = (当前页面-1) * 显示条数
						int statrNo = (currentPageNo - 1 ) * showSize;
						//调用e_book的getListBooks
						listBooks = e_book.getListBooks(n_categoryId, statrNo, showSize);
						//page类里面增添private List<Ebook_entry> bookList;
						page.setBookList(listBooks);
						return page;
	}
	
	
	//进入修改，返回一个使用ModelAndView modelAndView = new ModelAndView("modify");来跳转到modify页面，在把着页面返回给ajax请求
	@RequestMapping(value="/modify",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView  modify(@RequestParam(value="id") String id,HttpServletRequest request){
		Ebook_entry ebook = e_book.searchBook(id);
		request.setAttribute("book", ebook);
		ModelAndView modelAndView = new ModelAndView("modify");
		return modelAndView;
	}
	
	
	//完成修改并提交
	@RequestMapping(value="Dmodify",method=RequestMethod.GET)
	public String dmodify(Ebook_entry ebook_entry,
						@RequestParam(value="id",required=false) String id){
		logger.info("进入modify++++++++");
		//注意因为页面id为String类型，无法自动装配到pojo实体类里面的id，所以需转化int
		int c_id = Integer.valueOf(id);
		ebook_entry.setId(c_id);
		logger.info(ebook_entry.getCategoryId()+"aaaa"+ebook_entry.getSummary());
		if( e_book.modify(ebook_entry) ){
			return "redirect:/book/dobook";
		}else{
			return "JumeAddBook";	
		}
		
	}
	
	
	@RequestMapping(value="/delbook",method=RequestMethod.GET)
	@ResponseBody
	public String delbook(@RequestParam(value="id") String id){
		//HashMap<String,String> map = new HashMap();
		int e_id = Integer.valueOf(id);
		boolean flag = e_book.delBook(e_id);
		if(flag){
			
			//map.put("flag", "success");
			return "{\"flag\":\"success\"}";
		}else{
			return "{\"flag\":\"faild\"}";
		}
			//return map;
	}
	
	
	
	
	/*@RequestMapping(value="/delbook",method=RequestMethod.GET)
	@ResponseBody
	public HashMap<String,String> delbook(@RequestParam(value="id") String id){
		HashMap<String,String> map = new HashMap();
		int e_id = Integer.valueOf(id);
		boolean flag = e_book.delBook(e_id);
		if(flag){
			map.put("flag", "success");
			
		}else{
			map.put("flag", "success");
		}
			return map;
	}*/

	//进入到addbook增加页面，进入时候传Ebook_entry ebook_entry，当表单里面的数据名跟pojo里面的类的属性名一致能自动装配
	//@ModelAttribute  ：把ebook_entry放进Model.sestAttribute里面
	@RequestMapping(value="JumeAddBook")
	public String jumeAddBook(@ModelAttribute Ebook_entry ebook_entry){
		return "secAddBook";
	}
	
	//addbook提交的时候，方法括号里面的Ebook_entry ebook_entry，为JumeAddBook方法设到Model.sestAttribute里面Ebook_entry ebook_entry对象
	@RequestMapping(value="addBooks",method=RequestMethod.GET)
	public String addBooks(Ebook_entry ebook_entry){
		boolean flag = e_book.addBook(ebook_entry);
		if(flag){
			//直接ruturn "book" 主页,url地址栏会出来n多个参数，经过dobook方法来跳转到主页url就没有参数显示
			return "redirect:/book/dobook";
		}else{
			return "secAddBook";
		}
		
	}
	
	@RequestMapping(value="dobook")
	public String dobook(){
		return "book";
	}
	

}
